package com.swt.gap.controller;

import com.swt.gap.model.entity.SysDepartment;
import com.swt.gap.model.vo.SysDepartmentTreeVO;
import com.swt.gap.service.SysDepartmentService;
import com.swt.gap.service.SysUserService;
import io.swagger.annotations.*;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author sx
 */
@Api(tags = "102 部门管理")
@RestController
@RequestMapping("/department")
public class SysDepartmentController {

    private final SysDepartmentService sysDepartmentService;
    private final SysUserService sysUserService;

    public SysDepartmentController(SysDepartmentService sysDepartmentService, SysUserService sysUserService) {
        this.sysDepartmentService = sysDepartmentService;
        this.sysUserService = sysUserService;
    }

    @ApiOperation(value = "新增部门")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "SysDepartment", name = "department", required = true)
    })
    @ApiResponse(code = 201, message = "created", response = Integer.class)
    @PostMapping
    public ResponseEntity<Integer> addDepartment(@RequestBody SysDepartment department) {
        if (null == department.getParentDepartmentId()) {
            department.setParentDepartmentId(0);
        }
        return ResponseEntity.status(HttpStatus.CREATED).body(sysDepartmentService.addDepartment(department));
    }

    @ApiOperation(value = "获取单个部门")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "int", name = "departmentId", required = true)
    })
    @ApiResponse(code = 200, message = "ok", response = SysDepartment.class)
    @GetMapping("/{departmentId}")
    public ResponseEntity<SysDepartment> getDepartment(@PathVariable Integer departmentId) {
        return ResponseEntity.ok(sysDepartmentService.getDepartment(departmentId));
    }

    @ApiOperation(value = "获取部门树")
    @ApiResponse(code = 200, message = "ok", response = SysDepartmentTreeVO.class, responseContainer = "List")
    @GetMapping
    public ResponseEntity<List<SysDepartmentTreeVO>> listDepartment() {
        return ResponseEntity.ok(sysDepartmentService.listDepartment());
    }

    @ApiOperation(value = "修改部门")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "int", name = "departmentId", required = true),
            @ApiImplicitParam(paramType = "body", dataType = "SysDepartment", name = "department", required = true)
    })
    @ApiResponse(code = 200, message = "ok", response = Integer.class)
    @PutMapping("/{departmentId}")
    public ResponseEntity<Integer> updateDepartment(@PathVariable Integer departmentId, @RequestBody SysDepartment department) {
        department.setDepartmentId(departmentId);
        return ResponseEntity.ok(sysDepartmentService.updateDepartment(department));
    }

    @ApiOperation(value = "删除部门")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "int", name = "departmentId", required = true)
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = Integer.class),
            @ApiResponse(code = 440, message = "部门有下级部门"),
            @ApiResponse(code = 441, message = "部门有绑定用户")
    })

    @DeleteMapping("/{departmentId}")
    public ResponseEntity<Integer> deleteDepartment(@PathVariable Integer departmentId) {

        if (!CollectionUtils.isEmpty(sysDepartmentService.listChildrenDepartments(departmentId))) {
            return ResponseEntity.status(440).build();
        } else if (!CollectionUtils.isEmpty(sysUserService
                .listUserByDepartmentId(1, 10, null, departmentId).getList())){
            return ResponseEntity.status(441).build();
        }
        return ResponseEntity.ok(sysDepartmentService.deleteDepartment(departmentId));
    }
}
